<?php

require_once(dirname(__FILE__) . '/appconf.php');

class BlogApi extends Site {

    function setup() {
        $this->run_modes('hairBlogIndex', 'getBlog', 'recentPosts', 'getMemberInfo', 'imoreBlogIndex');
        $this->start_mode('hairBlogIndex');
        $this->selfurl = '/blog.php';
        parent::setup();
    }

    function cgiapp_prerun() {

        $this->HairBlogIndex = getModel('Model_HairBlogIndex');
        $this->BlogCategory = getModel('Model_BlogCategory');
        parent::cgiapp_prerun();
    }

    function action_imoreBlogIndex() {
        $type = $this->get_q("type", 1, 'int');
        $dateflag = $this->get_q("dateflag", 0, 'int');
		
		
        $kvcobj = getCache('imoreBlogIndex', array('expire' => KVC_EXPIRE));
        $_return_data = $kvcobj->get('imoreBlogIndex_' . $type . "_" . $dateflag);
		if (!$_return_data) {
			
			$datatimes = array(
				1=>array(time()-24*3600,time()),
				2=>array(time()-48*3600,time()-24*3600),
				3=>array(time()-72*3600,time()-48*3600)
			);
			$datas = array();
			$datas[1] = '"meling","jane-fiesta","leliachow","janey","koeysung","shirleyfoo","elawwong","lamyi","koby","ritzlam","jaden","joseph","willor","rose","daizyland","aninaho","kolenbut","rachellam","jamie318","kelvinkan","deilsiu","tabokiss"';
				//$type_mid_array[1] = "9082,12926,9069,8970,8971,12928,12929,9070,12930,9206";
			$datas[2] = '"mandytam","isischiu","dominique","monit","kellyliu","kauriyau"';
				//$type_mid_array[2] = "38,40,9194,9097,8120";
			$datas[3] = '"ben","vickiechan","dotdot","kaye","happy","kittyfung","yuetyee","blurryclaire","daisymarisfung","glamorouskam","alice","candy","candyyip","fungling","waterfish","helloiris","juneleung","snoopyvivi","annak","ahfa","berrychui","irene","katherine","mnwl","vinciyip","wengichow","amazinggrace","stellastella","gillianwai","juan","beautyhunter","wingmiracle","beckylau","mokbb","chanlailate","elyse214","heratoftheocean","manman811","wawa","juliaa","dingdingchan","maxine","siua","wkykam","chicmanman","cherryastor","mayel","cherrietam1204","yankiki","bibob","tatachow","moniquelau","popobb","smallbird1111","kammy41","celiaau","yeeki212","komichan"';
				//$type_mid_array[3] = "47,8020,403,4913,5132,5813,2718,6773,601,1299,6025,513,405,30,458,7302,7617,11335,9458,10454,197,11916,7148,8141,7096,101,8787,5683,5680,9104,12159,12074,12581,7914,10700,12348,11675,10599,198,12013,1494,2831,3838,1781";

			
			$h_param = array();
			$h_param['datas'] = $datas;
			$h_param['type'] = $type ? $type : 1;
			$h_param['dateflag'] = $dateflag ? $dateflag : 0;
			$result = $this->HairBlogIndex->getImoreBlogList($h_param);
		

			$return = $itrial_blog_name_arr = array();
			if ($result['result']) {
				foreach ($result['result'] as $key => $value) {
					$itrial_blog_name_arr[] = $value['blog_name'];
					/*
					  $pat = '/<img(.*?)src="\/upload\/(.*?)"(.*?)>/i';
					  $replacement = '<img${1}src="' . DOMAIN . 'upload/${2}"${3}>';
					  $value['body'] = preg_replace($pat, $replacement, htmlspecialchars_decode($value['body']));
					 */
					$return[$value['blog_name']] = $value;
				}
			}
			$blog_name_array = array (
			  'beautyhunter' => 263,
			  'beckylau' => 266,
			  'bibob' => 70,
			  'celiaau' => 286,
			  'chanlailate' => 267,
			  'cherryastor' => 259,
			  'chicmanman' => 277,
			  'dingdingchan' => 273,
			  'elyse214' => 268,
			  'heratoftheocean' => 270,
			  'juan' => 262,
			  'juliaa' => 272,
			  'kammy41' => 285,
			  'komichan' => 288,
			  'manman811' => 269,
			  'maxine' => 274,
			  'mayel' => 278,
			  'mokbb' => 265,
			  'moniquelau' => 281,
			  'popobb' => 282,
			  'siua' => 275,
			  'smallbird1111' => 283,
			  'tatachow' => 280,
			  'wawa' => 271,
			  'wingmiracle' => 264,
			  'wkykam' => 276,
			  'yankiki' => 113,
			  'yeeki212' => 287,
			  'pasco' => 290,
			  'tabokiss' => 260,
			  'aninaho' => 167,
			  'jane-fiesta' => 28,
			  'jamie318' => 204,
			  'daizyland' => 58,
			  'elawwong' => 48,
			  'janey' => 30,
			  'rachellam' => 201,
			  'deilsiu' => 257,
			  'willor' => 168,
			  'kolenbut' => 195,
			  'kelvinkan' => 180,
			  'ritzlam' => 166,
			  'jaden' => 170,
			  'rose' => 169,
			  'joseph' => 189,
			  'meling' => 6,
			  'koby' => 51,
			  'shirleyfoo' => 39,
			  'lamyi' => 49,
			  'leliachow' => 29,
			  'stellastella' => 110,
			  'isischiu' => 3,
			  'mandytam' => 2,
			  'dominique' => 52,
			  'vincy' => 191,
			  'yoko' => 293,
			  'crystal' => 292,
			  'cillap' => 291,
			  'percyfan' => 294,
			  'inggradsomething' => 295,
			);
			$datas_arr = explode(',', str_replace('"','',$datas[$type]));
			
			foreach($datas_arr as $k=>$v){
				$blog_arr_temp = array();
				if(!in_array($v, $itrial_blog_name_arr)){
					$param = array();
					$param['field'] = "blog_id";
					$param['where'] = " path ='/blog/".$v."/'";
					$_data = db_query_hash('wp_blogs', $param, IMOREBLOGDB);
					if($_data['result'][0]['blog_id']) $blog_name_array[$v] = $_data['result'][0]['blog_id'];
				}
			}
			foreach($datas_arr as $k=>$v){
				if(array_key_exists($v, $blog_name_array)){
					$param = array();
					$param['field'] = "id,post_date as addtime,post_title as title";
					$param['where'] = " post_status='publish' and post_type='post'";
					$param['order'] = " post_date desc";
					$param['limit'] = "1";
					$data = db_query_hash('wp_' . $blog_name_array[$v] . '_posts', $param, IMOREBLOGDB);
					$data['result'][0]['blog_name'] = $v;
					$data['result'][0]['is_wp'] = 1;
					$return[$v] = $data['result'][0];
				}
			}
			
			$returns =$_return_data = array();
			foreach($return as $k=>$v){
				$t = strtotime($v['addtime']);
				if($dateflag==0||($datatimes[$dateflag][0]<=$t&&$t<=$datatimes[$dateflag][1])){
					$returns[$t] = $v;
				}
			}
			$j=0;
			foreach($returns as $k=>$v){
				$_return_data[$j] = $v;
				$j++;
			}
			$kvcobj->set('imoreBlogIndex_' . $type . "_" . $dateflag, $_return_data);
        }
        $this->ajaxReturn($_return_data, array('retcode' => '1', 'msg' => 'ok'));
    }

    function action_hairBlogIndex() {
        //$page = $this->get_q("page", 1, 'int');
        $type = $this->get_q("type", 1, 'int');

        $kvcobj = getCache('hairBlogIndex', array('expire' => KVC_EXPIRE));
        $_return_data = $kvcobj->get('hairBlogIndex' . $type);
        if (!$_return_data) {
			$datas = array();
			$datas[1] = '"bibob","amyleung","heidilee","yankiki","joanie","kitty1230","kanix","sky0721","tiffanyyau","ice198322","viannkiko","kellychingwy","crazyahmin","bee","cindy214","coombe","siumbbx2","szekaijessicayu","fungling","amazinggrace","armui","lazycc","winwinc","luvyan1314","olsenbabi","cherryastor"';
			//$type_mid_array[1] = "2831, 1513, 1479, 1086, 1494, 9104, 3863, 2724, 738, 1781, 7635, 6022, 985, 1924, 5810, 8359, 7026, 4879, 7705, 212, 6276, 2207, 7536, 65, 3838,7268";
			$datas[2] = '"aninaho","ritzlam","willor","rose","vincy","rachellam"';
			//$type_mid_array[2] = "12926, 9069, 8970, 8971, 12927, 12928";
			$datas[3] = '"kelvinkan","kolenbut","jaden","joseph","williamchow","jamie318"';
			//$type_mid_array[3] = "9082,12929,9070,12930,12931,9206"; 
			
			$h_param['datas'] = $datas;
			//$h_param['page'] = $page ? $page : 1;
			$h_param['type'] = $type ? $type : 1;
			//$h_param['size'] = 8;
			$result = $this->HairBlogIndex->getList($h_param);
		

			$return = $itrial_blog_name_arr = array();
			if ($result['result']) {
				foreach ($result['result'] as $key => $value) {
					$itrial_blog_name_arr[] = $value['blog_name'];
					/*
					  $pat = '/<img(.*?)src="\/upload\/(.*?)"(.*?)>/i';
					  $replacement = '<img${1}src="' . DOMAIN . 'upload/${2}"${3}>';
					  $value['body'] = preg_replace($pat, $replacement, htmlspecialchars_decode($value['body']));
					 */
					$return[$value['blog_name']] = $value;
				}
			}
			$blog_name_array = array (
			  'beautyhunter' => 263,
			  'beckylau' => 266,
			  'bibob' => 70,
			  'celiaau' => 286,
			  'chanlailate' => 267,
			  'cherryastor' => 259,
			  'chicmanman' => 277,
			  'dingdingchan' => 273,
			  'elyse214' => 268,
			  'heratoftheocean' => 270,
			  'juan' => 262,
			  'juliaa' => 272,
			  'kammy41' => 285,
			  'komichan' => 288,
			  'manman811' => 269,
			  'maxine' => 274,
			  'mayel' => 278,
			  'mokbb' => 265,
			  'moniquelau' => 281,
			  'popobb' => 282,
			  'siua' => 275,
			  'smallbird1111' => 283,
			  'tatachow' => 280,
			  'wawa' => 271,
			  'wingmiracle' => 264,
			  'wkykam' => 276,
			  'yankiki' => 113,
			  'yeeki212' => 287,
			  'pasco' => 290,
			  'tabokiss' => 260,
			  'aninaho' => 167,
			  'jane-fiesta' => 28,
			  'jamie318' => 204,
			  'daizyland' => 58,
			  'elawwong' => 48,
			  'janey' => 30,
			  'rachellam' => 201,
			  'deilsiu' => 257,
			  'willor' => 168,
			  'kolenbut' => 195,
			  'kelvinkan' => 180,
			  'ritzlam' => 166,
			  'jaden' => 170,
			  'rose' => 169,
			  'joseph' => 189,
			  'meling' => 6,
			  'koby' => 51,
			  'shirleyfoo' => 39,
			  'lamyi' => 49,
			  'leliachow' => 29,
			  'stellastella' => 110,
			  'isischiu' => 3,
			  'mandytam' => 2,
			  'dominique' => 52,
			  'vincy' => 191,
			  'yoko' => 293,
			  'crystal' => 292,
			  'cillap' => 291,
			  'percyfan' => 294,
			  'inggradsomething' => 295,
			);
			$datas_arr = explode(',', str_replace('"','',$datas[$type]));
			
			foreach($datas_arr as $k=>$v){
				$blog_arr_temp = array();
				if(!in_array($v, $itrial_blog_name_arr)){
					$param = array();
					$param['field'] = "blog_id";
					$param['where'] = " path ='/blog/".$v."/'";
					$_data = db_query_hash('wp_blogs', $param, IMOREBLOGDB);
					if($_data['result'][0]['blog_id']) $blog_name_array[$v] = $_data['result'][0]['blog_id'];
				}
			}
			foreach($datas_arr as $k=>$v){
				if(array_key_exists($v, $blog_name_array)){
					$param = array();
					$param['field'] = "id,post_date as addtime,post_title as title";
					$param['where'] = " post_status='publish' and post_type='post'";
					$param['order'] = " post_date desc";
					$param['limit'] = "1";
					$data = db_query_hash('wp_' . $blog_name_array[$v] . '_posts', $param, IMOREBLOGDB);
					$data['result'][0]['blog_name'] = $v;
					$data['result'][0]['is_wp'] = 1;
					$return[$v] = $data['result'][0];
				}
			}
			
			$returns = $_return_data = array();
			foreach($return as $k=>$v){
				if(!empty($v['title'])){
					$t = strtotime($v['addtime']);
					$returns[$t] = $v;
				}
			}
			krsort($returns);
			$j=0;
			foreach($returns as $k=>$v){
				$_return_data[$j] = $v;
				$j++;
			}
		    $kvcobj->set('hairBlogIndex' . $type, $_return_data);
        }
        $this->ajaxReturn($_return_data, array('retcode' => '1', 'msg' => 'ok'));
    }

    function action_getBlog() {
        $h_param = array();
        $blog_name = $this->get_q("blog_name");
        $blog_id = $this->get_q("blog_id", 0, 'int');
        $category = $this->get_q("category", 0, 'int');
        $h_param['blog_id'] = $blog_id ? $blog_id : 0;
        $h_param['category'] = $category ? $category : 0;
        $h_param['blog_name'] = $blog_name ? $blog_name : "";

        $result = $this->HairBlogIndex->getBlog($h_param);
        $data = array();
        if ($result) {
            foreach ($result as $key => $value) {

                $pat = '/<img(.*?)src="\/upload\/(.*?)"(.*?)>/i';
                $replacement = '<img${1}src="' . DOMAIN . 'upload/${2}"${3}>';
                $value['body'] = preg_replace($pat, $replacement, htmlspecialchars_decode($value['body']));

                $data[$key] = $value;
            }
        }
        $this->ajaxReturn($data, array('retcode' => '1', 'msg' => 'ok'));
    }

    function action_recentPosts() {
        $h_param = array();
        $blog_name = $this->get_q("blog_name");
        $h_param['blog_name'] = $blog_name ? $blog_name : "";

        $result = $this->HairBlogIndex->getRecentPosts($h_param);
        $this->ajaxReturn($result, array('retcode' => '1', 'msg' => 'ok'));
    }

    function action_getMemberInfo() {
        $result = array();
        $h_param = array();
        $blog_name = $this->get_q("blog_name");
        $h_param['blog_name'] = $blog_name ? $blog_name : "";

        $memberInfo = $this->HairBlogIndex->getMemberInfo($h_param);
        if ($memberInfo) {
            $result['blogdescription'] = $memberInfo['blogdescription'];
            $result['blog_name'] = $memberInfo['blog_name'];
        }

        $h_param = array(
            'mid' => $memberInfo['mid'],
        );
        $blogCategory = $this->BlogCategory->getList($h_param);
        // if ($blogCategory['result'])
        $result['blogCategory'] = $blogCategory;

        //echo '<pre>' . var_export($result, 1) . '</pre>';
        $this->ajaxReturn($result, array('retcode' => '1', 'msg' => 'ok'));
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new BlogApi();
$webapp->run();
?>